package 极客算法训练营.chapter07;

public class 二叉树的最大深度 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution2 {
        private int level;
        public int maxDepth(TreeNode root) {
            generate(root, 0);
            return level;
        }

        public void generate(TreeNode root, int currentlevel) {
            if (root == null) {
                return;
            }
            if (++currentlevel > level) level = currentlevel;
            generate(root.left, currentlevel);
            generate(root.right, currentlevel);
        }
    }

    class Solution {
        public int maxDepth(TreeNode root) {
            return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    }
}
